package com.yowasa.xiuxian.engine.message.hone;


import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.service.helper.UserTagHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;

@CMD(name = "/突破", scope = CmdScopeEnum.ALL)
public class BreakHandler extends CmdMessageHandler {

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserTagHelper userTagHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUserAll(message);

        Integer code = user.getStage();
        Integer level = user.getLevel();
        if (level != 100) {
            throw new HandlerException("你当前境界还没有修炼至圆满");
        }
        if (code == 9) {
            user.setLevel(101);
            userHelper.saveUserInfo(user);
            userTagHelper.addOne(user.getUserId(), XiuxianConstants.TAG_FEISHENG);
//            userHelper.delUser(user);
            messageHelper.replyMys(message, "恭喜你历经千辛万苦最终修炼成仙!"+"\n天翻地覆任我行\n千锤百炼练我形\n长夜漫漫泣苦辛\n登顶高峰终成仙\n\n你的境界已修炼圆满，你现在可以选择转生了");
            return;
        }
        StageEnum stage = StageEnum.getById(code+1);
        int rate = 200 * (stage.getRate() -1);
        if(user.getUserConfig().getTravel() < rate){
            int i = rate - user.getUserConfig().getTravel();
            throw new HandlerException("枯坐苦修容易走火入魔，只有经历磨练才能一往无前！\n你还需要积攒"+i+"点历练值才可突破");
        }
        user.getUserConfig().setTravel(user.getUserConfig().getTravel() - rate);
        MentalInterface mental = HoneEngine.mentalMap.get(user.getUserConfig().getMental());
        //心法突破属性提升
        String msg = mental.breakUp(user);
        //重构建战斗属性,神通和装备对面板的影响
        userHelper.rebuildBattleInfo(user);
        userHelper.saveUserInfo(user);
        messageHelper.replyMys(message, MessageFormat.format("突破到{0}成功！战斗能力大幅度提升!\n提升属性：\n{1}", stage.getName(), msg));
    }

    public String desc(){
        return new StringBuffer().append("当你修炼到境界圆满之时，可以用这个指令突破境界，获得战斗能力提升\n")
                .append("每个人突破境界带来的属性加成都不相同，会按你的基础面板和随机数进行计算")
                .toString();

    }
}
